<template>
  <d2-container>
    <el-form :inline="true" :model="dataSearch" ref="dataSearch">
      <el-form-item label="员工姓名" prop="staff">
        <el-input v-model="dataSearch.staff" placeholder="请输入员工姓名"></el-input>
      </el-form-item>

      <el-form-item label="审批状态" prop="status">
        <el-radio-group v-model="dataSearch.status">
          <el-radio :label="-1">取消</el-radio>
          <el-radio :label="0">审批中</el-radio>
          <el-radio :label="2">审批同意</el-radio>
          <el-radio :label="3">审批未通过</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="formSubmit">
          <d2-icon name="search" />查询
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="formReset">
          <d2-icon name="refresh" />重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-table :data="dataList" border style="width: 100%">
      <el-table-column prop="staff.name" label="申请员工"></el-table-column>
      <el-table-column prop="school.name" label="校区"></el-table-column>

      <el-table-column prop="reason" label="请假理由"></el-table-column>
      <el-table-column prop="status" label="请假状态"></el-table-column>
      <el-table-column prop="remark" label="请假备注"></el-table-column>
      <el-table-column prop="startTime" label="请假开始时间"></el-table-column>
      <el-table-column prop="endTime" label="请假结束时间"></el-table-column>

      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button @click="detail(scope.row)" type="text" size="small">查看详情</el-button>
          <el-button @click="handleAgreeClick(scope.row)" type="text" size="small" v-show="scope.row.status == '审批中'">同意</el-button>
          <el-button @click="handleDisagreeClick(scope.row)" type="text" size="small" v-show="scope.row.status == '审批中'">拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="total,sizes,prev, pager, next,jumper"
      :page-size="pageSize"
      :current-page="currentPage"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handlerSizeChange"
      @next-click="handleNextPage"
      @prev-click="handlePrevClick"
    ></el-pagination>
  </d2-container>
</template>

<script>
import request from "@/plugin/axios";
import {mapState} from "vuex";

export default {
  data() {
    return {
      dataList: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      classroom: [],
      dataSearch: {}
    };
  },
  computed: {
    ...mapState('d2admin/user', ['info']),
  },
  methods: {
    formSubmit() {
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    formReset() {
      this.$refs.dataSearch.resetFields();
    },

    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    handlerSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    handleNextPage() {
      this.currentPage = this.currentPage + 1;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    handlePrevClick() {
      this.currentPage = this.currentPage - 1;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    handleAgreeClick(e) {
      this.$confirm("此操作审批通过, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          request({
            url: "/leaveRecord/updateLeaveRecord",
            method: "post",
            data: { _id: e._id, status: 2 }
          }).then(res => {
            if (res.code == 200) {
              this.$message({
                message: "操作成功"
              });
              this.getDataList(this.dataSearch);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    handleDisagreeClick(e) {
      this.$confirm("此操作拒绝审批通过, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          request({
            url: "/leaveRecord/updateLeaveRecord",
            method: "post",
            data: { _id: e._id, status: 3 }
          }).then(res => {
            if (res.code == 200) {
              this.$message({
                message: "操作成功"
              });
              this.getDataList(this.dataSearch);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    detail(e) {
      this.$router.push({ path: "/leave-record-detail?id=" + e._id });
    },

    getDataList(data) {
      this.dataSearch.limitSchool = this.info.school[0]._id;
      data.limitSchool = this.dataSearch.limitSchool;
      data.name = this.dataSearch.staff
      request({
        url: "/leaveRecord/getLeaveRecordList",
        method: "post",
        data: data
      }).then(res => {//审批状态 -1已取消 0申请中 2同意 3拒绝
        console.log(res)
        this.dataList = [];
        this.dataList = res.data.leaveRecord;
        this.dataList.forEach((item, index) => {
          let status = parseInt(item.status);
          this.dataList[index].status =
            status === 0
              ? "审批中"
              : status === -1
              ? "已取消"
              : status === 2
              ? "审批通过"
              : status === 3
              ? "审批未通过"
              : "未知状态";
          // this.dataList[index].staff = item.staff.name;
          // this.dataList[index].school = item.staff.school.name;
          this.dataList[index].startTime = this.formatDate(item.applyTime.startTime);
          this.dataList[index].endTime = this.formatDate(item.applyTime.endTime);
        });
        this.total = res.data.total;
        this.currentPage = res.data.currentPage;
      });
    },
    formatDate(date) {
      var date = new Date(date);
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      return [year, month, day].map(this.formatNumber).join("-");
    },
    formatNumber(n) {
      n = n.toString();
      return n[1] ? n : "0" + n;
    },
    load(){
      this.school = this.info.school[0]._id
    this.dataSearch.school = this.school
    this.dataSearch.page = this.currentPage;
    this.dataSearch.pageSize = this.pageSize;
    this.getDataList(this.dataSearch);
    }
  },
    beforeRouteEnter(to, from, next) {
            if (to.meta.refresh == 'a'){
                to.meta.refresh = undefined
                next(v=>{
                v.load()
                })
            }
            else{
                next()
            }


        },
        beforeRouteLeave(to,from,next){
            next()
        },
  created() {
    this.load()
  }
};
</script>
